package com.lu.aio.client;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.lu.aio.ChannelAdapter;
import com.lu.aio.ChannelHandler;
import lombok.extern.slf4j.Slf4j;

import java.nio.channels.AsynchronousSocketChannel;
import java.util.Date;

/**
 * @program lu-netty
 * @description:
 * @author: zhanglu
 * @create: 2022-06-10 17:45:00
 */
@Slf4j
public class AioClientHandler extends ChannelAdapter {

    public AioClientHandler(AsynchronousSocketChannel channel, String charset) {
        super(channel, charset);
    }

    @Override
    protected void channelActive(ChannelHandler ctx) {
        ctx.writeAndFlush("hi！小鲁哥~");
    }

    @Override
    protected void channelInactive(ChannelHandler ctx) {

    }

    @Override
    protected void channelRead(ChannelHandler ctx, Object msg) {
        String currentDate = DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN);
        log.info(StrUtil.format("{} --- 接收消息：{}", currentDate, msg));
    }
}
